package com.wudeyong;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Author :wudeyong
 * Date 2017/7/3
 * For More Information,Please Visit https://wudeyong.com
 */
public class HierarchicalClustering {

    private double[][] data=new double[150][4];
    ArrayList<Integer> iii=new ArrayList<Integer>();

    public static void main(String [] args){
        HierarchicalClustering hierarchicalClustering=new HierarchicalClustering();
        hierarchicalClustering.getDataFromFile("C:\\Users\\TeenTeam\\Desktop\\大数据\\data.txt");
        double result[][]=hierarchicalClustering.calculate();
        System.out.println(hierarchicalClustering.iii.toString());
        System.out.println(result);
    }

    private double[][] calculate(){
        double[][] tmpData=new double[150][4];

        for(int l=0;l<data.length;l++){
            System.arraycopy(data[l],0,tmpData[l],0,4);
        }
        double[] newCore;
//        ArrayList[] coreList=new ArrayList[4];
        int k=150;
        for(k=150;k>4;k--){
            double min=calculateDistance(tmpData[0],tmpData[1]);
            int[] minPosition={0,0};
            for(int i=0;i<k;i++){
                for(int j=i+1;j<k;j++)
                {
                    double a1=calculateDistance(tmpData[i],tmpData[j]);
                    if (a1<min){
                        min=a1;
                        minPosition[0]=i;
                        minPosition[1]=j;

                    }
                }
            }
            iii.add(minPosition[0]);
            iii.add(minPosition[1]);

//            if(minPosition[0]<minPosition[1])
//            {
//                int tmpA=minPosition[0];
//                minPosition[0]=minPosition[1];
//                minPosition[1]=tmpA;
//            }
//            if()
            double [] tmpArray1=tmpData[minPosition[0]];
            double [] tmpArray2=tmpData[minPosition[1]];
            System.arraycopy(tmpData, minPosition[0] + 1, tmpData, minPosition[0], minPosition[1] - minPosition[0]-1);
            if(k -1- (minPosition[1] + 1)>0){
                System.arraycopy(tmpData, minPosition[1] + 1, tmpData, minPosition[1] - 1, k -1- (minPosition[1] + 1));
            }

            tmpData[k-2]=combine(tmpArray1,tmpArray2);
        }
        return tmpData;

//        newCore=combine(data[minPosition[0]],data[minPosition[1]]);
//        data[minPosition[0]]=returnValue(newCore);
//        data[minPosition[1]]=returnValue(newCore);



    }

//    private double[] returnValue(double[] a){
//        double[] b=new double[a.length];
//        System.arraycopy(a, 0, b, 0, a.length);
//        return b;
//    }

    private double[] combine(double[] a,double[] b){
        double[] tmp=new double[a.length];
        for(int i=0;i<a.length;i++)
            tmp[i]=(a[i]+b[i])/2;
        return tmp;

    }

    /**
     * 计算空间中欧氏距离
     * @param x 点1
     * @param y 点2
     * @return 欧氏距离
     */
    private double calculateDistance(double[] x, double[] y){
        int length=x.length;
        double sum=0.f;
        for(int i=0;i<length;i++){
            sum=(x[i]-y[i])*(x[i]-y[i])+sum;
        }
        return Math.sqrt(sum);
    }

    /**
     * 从文件读取信息
     * @param filePath 文件路径
     */
    public void getDataFromFile(String filePath){

        try {
            FileInputStream in=new FileInputStream(filePath);
            BufferedReader reader=new BufferedReader(new InputStreamReader(in));
            String tmp;
            int i=0;
            while ((tmp=reader.readLine())!=null){
                String tmpArra[]=tmp.split("\t");
                for(int j=0;j<tmpArra.length;j++)
                    data[i][j]=Double.parseDouble(tmpArra[j]);
                i++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
